package cn.roadtrip.mtravelbusinessmodule.util.api.contract;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpUtil;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 合同签名api
 */
@Component
@Slf4j
public class ContractImpl {
//
    private String appId = "2a41f0809d9011ee8e595594d5787c6d";
    private String signKey = "247bd465a02046c086b68e60c9ffac7c";
//    private String appId = "eccc1960900011ea87789bc5be87fc13";
//    private String signKey = "32d5b17353354eca919b682d6f9c66aa";

//    private String url = "http://open-api.stage.tourage.cn";
    private String url = "http://econtract-api.stage.tourage.cn";

    /**
     * 创建签名
     *
     * @param timestamp
     * @return
     */
    public String createSinature(Long timestamp) {
        String signStr = signKey + timestamp;
        String sinature = MD5.create().digestHex(signStr);
        return Base64.encode(sinature);
    }

    @PostConstruct
    public void init() {
        try {
            long time = System.currentTimeMillis();
            String sinature = createSinature(time);
            String rs = HttpUtil.createGet(url + "/v1/econtract/EC98DKFJD99EO9")
                    .header("Content-Type", "application/json")
                    .header("x-12301-source", appId)
                    .header("x-12301-key", appId)
                    .header("x-12301-version", "1.1.1")
                    .header("x-12301-timestamp", "" + time)
                    .header("x-12301-signature", sinature)
                    .timeout(4000)
                    .execute()
                    .body();
            log.info("【旅游合同】初始key返回结果：{}", rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
